package com.itheima.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.itheima.pojo.OssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

//阿里存储工具类
    @Component
    @Import(OssProperties.class)//导入，相当于将OssProperties对象放入容器
    public class OssTemplate {

        @Autowired
        private OssProperties ossProperties;

        //文件上传
        public String upload(String fileName, InputStream inputStream) {
            //创建客户端
            OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getKey(), ossProperties.getSecret());

            //设置文件最终的路径和名称
            String objectName = "images/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date())
                    + "/" + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

            //meta设置请求头,解决访问图片地址直接下载
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));

            //上传
            ossClient.putObject(ossProperties.getBucket(), objectName, inputStream, meta);

            //关闭客户端
            ossClient.shutdown();

            return "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + objectName;
        }

        //文件后缀处理
        private String getContentType(String FilenameExtension) {
            if (FilenameExtension.equalsIgnoreCase(".bmp")) {
                return "image/bmp";
            }
            if (FilenameExtension.equalsIgnoreCase(".gif")) {
                return "image/gif";
            }
            if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                    FilenameExtension.equalsIgnoreCase(".jpg") ||
                    FilenameExtension.equalsIgnoreCase(".png")) {
                return "image/jpg";
            }
            return "image/jpg";
        }
    }

